﻿using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZoneLineNew.Enums;
using ZoneLineNew.Models.Dto;
using ZoneLineNew.Models.Entity.BaseInfo;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;
using ZoneLineNew.ViewModelBase;
using ZoneLineNew.Views.Dialogs;

namespace ZoneLineNew.ViewModels {

    public class ProductViewModel : NavigationViewModel {
        public ProductViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate
            ) : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {

        }

        private string searchText;

        public string SearchText {
            get { return searchText; }
            set { searchText = value; base.RaisePropertyChanged(); }
        }

        private ObservableCollection<ProductDto> products = new ObservableCollection<ProductDto>();

        public ObservableCollection<ProductDto> Products {
            get { return products; }
            set { products = value; base.RaisePropertyChanged(); }
        }


        public DelegateCommand SearchCommand => new DelegateCommand(() => {
            LoadData();
        });

        public DelegateCommand AddCommand => new DelegateCommand(() => {
            DialogParameters keyValuePairs = new DialogParameters();
            keyValuePairs.Add("Title", "新增产品");
            _dialog.ShowDialog(nameof(EditProductDialogView), keyValuePairs, (rhs) => {
                if (rhs.Result == ButtonResult.OK) {
                    LoadData();
                }
            });

        });

        public DelegateCommand<ProductEntity> EditCommand => new DelegateCommand<ProductEntity>((arg) => {
            if (arg != null) {
                
                DialogParameters keyValuePairs = new DialogParameters();
                keyValuePairs.Add("Title", "编辑产品");
                keyValuePairs.Add("ProductEntity", JsonConvert.SerializeObject(arg));
                _dialog.ShowDialog(nameof(EditProductDialogView), keyValuePairs, (rhs) => {
                    if (rhs.Result == ButtonResult.OK) {
                        LoadData();
                    }
                });
            }
        });

        public DelegateCommand<ProductEntity> DeleteCommand => new DelegateCommand<ProductEntity>((arg) => {
            if (arg != null) {
                var dialogAsk = _message.Ask("确定是否删除此数据？");
                if (dialogAsk == System.Windows.MessageBoxResult.OK) {
                    var res = _mainDbOperate.DeleteModel<ProductEntity>(o => o.Id == arg.Id);
                    if (res) {
                        LoadData();
                    }
                }
            }
        });

        protected override void OnViewLoaded() {
            LoadData();
            //base.OnViewLoaded();
        }

        private void LoadData() {
            var list = _mainDbOperate.GetFreeSql().Select<ProductEntity, CustomerEntity>()
                .LeftJoin((a, b) => a.CustomerID == b.Id)
                .WhereIf(!string.IsNullOrEmpty(SearchText), (a, b) => a.ProductName.Contains(SearchText))
                .ToList((a, b) => new ProductDto {
                    ProductName = a.ProductName,
                    Description = a.Description,
                    Id = a.Id,
                    Model = a.Model,
                    Memo = a.Memo,
                    WorkDirection = a.WorkDirection,
                    MaterialNumber = a.MaterialNumber,
                    Unit = a.Unit,
                    CustomerID = b.Id,
                    CustomerName = b.Name,
                });
            Products.Clear();
            Products.AddRange(list);
        }

    }
}
